package com.cms.service;

import java.util.List;
import java.util.Scanner;

import com.cms.CRUD.FileOperate;
import com.cms.exception.WrongIdException;
import com.cms.model.Customer;

/**
 *  菜单类
 * 
 * @author 祝自豪
 * @date2019年8月15日
 */
public class Menu {

	private FileOperate fileOperate = FileOperate.getInstance();
	
	private  static Menu menu=new Menu();
	
	private Menu(){}
	
	public static Menu getInstance() {
		return menu;
	}
	

	/*
	 * 主菜单显示
	 */
	public void showMenu() {
		System.out.print("客户管理系统\r\n" + "**********************\r\n" + "[1]增加\r\n" + "[2]删除\r\n" + "[3]修改\r\n"
				+ "[4]查询\r\n" + "[5]浏览\r\n" + "[0]退出\r\n" + "**********************\r\n" + "请选择操作：");
	}

	public void subMenu() {
		System.out.print("〖查询客户〗\r\n" + "	[1]按编号查询\r\n" + "	[2]按姓名查询\r\n" + "	[3]按地址查询\r\n" + "	[4]退出\r\n"
				);
	}

	public void mainMenu1(Scanner scanner) {
		String id;
		String name;
		String address;
		System.out.print("请输入编号：");
		id = scanner.next();
		System.out.print("请输入名称：");
		name = scanner.next();
		System.out.print("请输入地址：");
		address = scanner.next();

		Customer customer = new Customer(id, name, address);

		fileOperate.insert(customer);
		System.out.println("新增成功");

	}

	public void mainMenu2(Scanner scanner) {
		String id;
		System.out.print("请输入要删除的编号：");
		id = scanner.next();
		try {
			fileOperate.delete(id);
			System.out.println("删除成功");
		} catch (WrongIdException e) {
			System.out.println(e.getMessage());
		}
		
	}

	public void mainMenu3(Scanner scanner) {
		String id;
		String name;
		String address;
		System.out.print("请输入编号：");
		id = scanner.next();
		System.out.print("请输入名称：");
		name = scanner.next();
		System.out.print("请输入地址：");
		address = scanner.next();

		Customer customer = new Customer(id, name, address);
		try {
			fileOperate.update(customer);
			System.out.println("修改成功");
		} catch (WrongIdException e) {
			System.out.println(e.getMessage());
		}
		
	}

	public void mainMenu4(Scanner scanner) {
		subMenu();
		while (true) {
			System.out.print("	" + "请输入查询编号:");
			String sid = scanner.next();
			if(Integer.parseInt(sid)==4) {
				break;
			}
			System.out.print("	" + "请输入查询数据:");
			String value = scanner.next();
			try {
				List<Customer> list=fileOperate.searchByCondition(sid, value);
				if(list.size()==0) {
					System.out.println("查询无记录");
				}else {
					showCustomers(list);
				}
			}catch (Exception e) {
			     System.out.println("输入有误");
			}
		}
	}

	public void mainMenu5() {
		showCustomers(fileOperate.showAll());
	}

	public void showCustomers(List<Customer> customers) {
		System.out.println("结果     " + "编号" +"\t\t" + "名称" +"\t" + "地址");
		for (Customer customer : customers) {
			System.out.println("    " + customer.getId() + " "+"\t" + customer.getName() + "\t" + customer.getAddress());
		}
		System.out.println("**********************");
	}

	public void start() {
		Scanner scanner;
		showMenu();
		while (true) {
			scanner = new Scanner(System.in);
			String key = scanner.next();
			if(key.equals("0"))
			{
				System.out.println("程序退出");
				break;
			}
			
			switch (key) {
			case "1":
				mainMenu1(scanner);
				break;
			case "2":
				mainMenu2(scanner);
				break;
			case "3":
				mainMenu3(scanner);
				break;
			case "4":
				mainMenu4(scanner);
				break;
			case "5":
				mainMenu5();
				break;
			
			default:
				System.out.println("输入的数字有误！");
				break;
			}
			try {
				System.out.println("三秒后显示菜单");
				Thread.sleep(3000);
				showMenu();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			
		}
	}
}
